﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperBasic.FrontEnd.Inter;
using SuperBasic.FrontEnd.Lexer;
using SuperBasic.FrontEnd.Parser;
using System.Diagnostics;
using SuperBasic.FrontEnd.Errors;

namespace SuperBasic
{
    class Program
    {

        static void Main(string[] args)
        {
            string path = @"F:\Projects\SuperBasic\Example\";
            string example = Path.Combine(path, "Example.sb");
            string output = Path.Combine(path, "Example.tsb");
            LexicalAnalyzer lexer;
            Parser parser;
            IntermediateCode codes; 
            lexer = new LexicalAnalyzer(path);
            Stopwatch time = new Stopwatch();
            Console.WriteLine("SATRT PASS1");
            time.Start();
            if (lexer.LoadSource(File.ReadAllText(example)))
            {
                time.Stop();
                //foreach (var item in lexer.Tokens)
                //{
                //    if(item.TokenTag == FrontEnd.Symbols.Tag.LINE_END)
                //    {
                //        Console.WriteLine();
                //    }
                //    else
                //    {
                //        Console.Write(item.TokenTag + " ");
                //    }
                //}
                Console.WriteLine("\tComplete. Time elapsed:" + time.ElapsedMilliseconds + " ms");
                Console.WriteLine("\t" + lexer.Tokens.Count() + " Tags Loaded.");
                Console.WriteLine("End PASS1\n");
                codes = new IntermediateCode();
                time.Reset();
                Console.WriteLine("SATRT PASS2");
                bool success = false;
                try
                {
                    time.Start();
                    parser = new Parser(lexer);
                    parser.Program();
                    time.Stop();
                    Console.WriteLine("\tComplete. Time elapsed:" + time.ElapsedMilliseconds + " ms");
                    success = true;
                }
                catch (Exception e)
                {
                    Console.WriteLine("\t错误：" + e.Message);
                }

                if (success)
                {
                    Console.WriteLine("\tGenerated " + IntermediateCode.CurrentIO.ToString().Length * 2  + " Bytes Code");
                }

                Console.WriteLine("End PASS2\n");
            }
            else
            {
                Console.WriteLine("解析失败！");
            }


            //Console.WriteLine();
            File.WriteAllText(output, IntermediateCode.CurrentIO.ToString());
            Console.ReadLine();
        }
    }
}
